﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.DirectX;

namespace NGE
{
    [Serializable]
    public class NAnimation : NFile
    {
        public NAnimation()
        {
            Frames = new List<NImage>();
        }

        //[NonSerialized]
        //public Vector3 LocationFrom;
        //[NonSerialized]
        //public Vector3 LocationTo;
        //[NonSerialized]
        //public int PlayedTimes;

        /// <summary>
        /// unit ms
        /// </summary>
        //[NonSerialized]
        //public int FlyTime;
        [NonSerialized]
        public int CurrentFrameIndex = 0;
        //public Vector3 GetLocationNow(double absTime)
        //{
        //    double flyTime = FlyTime / 1000.0;

        //    double perc = absTime - FirstVisitTime;
        //    perc /= flyTime;

        //    Vector3 ln = LocationFrom;
        //    ln.X += (float)(perc * (LocationTo.X - LocationFrom.X));
        //    ln.Y += (float)(perc * (LocationTo.Y - LocationFrom.Y));
        //    ln.Z += (float)(perc * (LocationTo.Z - LocationFrom.Z));

        //    ln.X -= Offset.X;
        //    ln.Y -= Offset.Y;
        //    ln.Z -= Offset.Z;
        //    return ln;
        //}

        List<NImage> Frames = new List<NImage>();
        List<int> FramesDelay = new List<int>();

        public NImage this[int i]
        {
            get
            {
                return Frames[i];
            }
        }
        public NImage CurrentFrame
        {
            get
            {
                return Frames[CurrentFrameIndex];
            }
        }
        public int CurrentFrameDelay
        {
            get
            {
                return FramesDelay[CurrentFrameIndex];
            }
        }
        public Vector3 Offset;



        public int FramesCount
        {
            get
            {
                return Frames.Count;
            }
        }
        /// <summary>
        /// unit ms
        /// </summary>
        public int Length
        {
            get
            {
                int ms = 0;
                foreach (int t in FramesDelay)
                {
                    ms += t;
                }
                return ms;
            }
        }
        [NonSerialized]
        public bool Pause;

        /// <summary>
        /// unit sec
        /// </summary>
        [NonSerialized]
        public double LastFrameTime;
        //[NonSerialized]
        //public double FirstVisitTime;

        public void AddFrame(NImage img, int delay_ms)
        {
            FramesDelay.Add(delay_ms);
            Frames.Add(img);
        }
        public void SetFrame(int index, int delay)
        {
            FramesDelay[index] = delay;
        }
        public void SetFrame(int index, NImage img, int delay_ms)
        {
            FramesDelay[index] = delay_ms;
            Frames[index] = img;
        }
        public void InsFrame(int index, NImage img, int delay_ms)
        {
            FramesDelay.Insert(index, delay_ms);
            Frames.Insert(index, img);
        }
        public void DelFrame(int index)
        {
            Frames.RemoveAt(index);
            FramesDelay.RemoveAt(index);
        }
        public void GotoFrame(int index)
        {
            CurrentFrameIndex = index;
        }

        //public void NextFrame(double absTime)
        //{
        //    if (Pause)
        //    {
        //        return;
        //    }
        //    //CurrentFrameIndex++;
        //    //if (CurrentFrameIndex == FramesCount)
        //    //{
        //    //    FirstVisitTime = absTime;
        //    //    CurrentFrameIndex = 0;
        //    //    PlayedTimes++;
        //    //}
        //    if (CurrentFrameIndex < FramesCount - 1)
        //    {
        //        CurrentFrameIndex++;
        //    }
        //    else
        //    {
        //        FirstVisitTime = absTime;
        //        PlayedTimes++;
        //        Pause = true;
        //    }
        //}
        public void NextFrame()
        {
            if (Pause)
            {
                return;
            }
            //CurrentFrameIndex++;
            //if (CurrentFrameIndex == FramesCount)
            //{
            //    FirstVisitTime = absTime;
            //    CurrentFrameIndex = 0;
            //    PlayedTimes++;
            //}
            if (CurrentFrameIndex < FramesCount - 1)
            {
                CurrentFrameIndex++;
            }
        }
        public void NextFrameLoop()
        {
            if (Pause)
            {
                return;
            }
            //CurrentFrameIndex++;
            //if (CurrentFrameIndex == FramesCount)
            //{
            //    FirstVisitTime = absTime;
            //    CurrentFrameIndex = 0;
            //    PlayedTimes++;
            //}
            if (CurrentFrameIndex < FramesCount - 1)
            {
                CurrentFrameIndex++;
            }
            else
            {
                Reset();
            }
        }

        public void Reset()
        {
            CurrentFrameIndex = 0;
        }

        public void Heartbeat(double abstime,bool loop)
        {
            if (abstime - LastFrameTime > ((double)CurrentFrameDelay / 1000.0))
            {
                LastFrameTime = abstime;
                if (loop)
                {
                    NextFrameLoop();
                }
                else
                {
                    NextFrame();
                }
            }
            
        }
    }
}
